package com.weilive.api.service;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.weilive.api.ServerConfig;
import com.weilive.api.script.JsScriptSystem;
import com.weilive.core.utils.SensitiveWordUtils;

/**
 * 黑名单管理系统
 * 
 * @author houzhiwei
 *
 */
@Service
public class BlackSystem {

	@Autowired
	private JsScriptSystem jsScriptSystem;

	private static final String FILE = "black/black.txt";
	private List<String> blackList;
	private String version;

	@PostConstruct
	public void init() {
		loadTXT();// 资源文件
		loadJS();
	}

	public String getVersion() {
		return this.version;
	}

	public List<String> getBlackList() {
		return this.blackList;
	}

	private void setBlackList(List<String> blackList) {
		this.blackList = blackList;
	}

	private void setVersion(String version) {
		this.version = version;
	}

	//
	private void loadTXT() {
		String file = ServerConfig.getScriptPath() + FILE;
		String version = "";
		List<String> blackList = new ArrayList<String>();
		try {
			BufferedReader reader = new BufferedReader(new FileReader(file));
			String line;
			while ((line = reader.readLine()) != null) {
				line = line.trim();
				if (line.length() == 0 || line.startsWith("#")) {
					continue;
				}

				int pos = line.indexOf("=");

				if (pos >= 0) {
					// String key = pos >= 0 ? line.substring(0, pos).trim() :
					// line;
					version = pos >= 0 ? line.substring(pos + 1).trim() : "";
				} else {
					blackList.add(line.trim());
				}
			}
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.setVersion(version);
		this.setBlackList(blackList);
	}

	private void loadJS() {
		List<String> blackList = new ArrayList<String>();
		List<String> blackNameList = new ArrayList<String>();

		jsScriptSystem.jsRunFun("black/black.js", "getBlack", new Object[] { blackList });
		jsScriptSystem.jsRunFun("black/black.js", "getBlackName", new Object[] { blackNameList });

		SensitiveWordUtils illegaWord = new SensitiveWordUtils();// 关键字过滤
		SensitiveWordUtils illegaName = new SensitiveWordUtils();// 名字中不能包含的
		// --> bigbear start
		for (String word : blackList) {
			illegaWord.addSensitiveWord(word);
		}
		for (String word : blackNameList) {
			illegaName.addSensitiveWord(word);
		}
		// 过滤掉
		for (String word : getBlackList()) {
			illegaName.addSensitiveWord(word);
			illegaWord.addSensitiveWord(word);
		}
		setIllegaWord(illegaWord);
		setIllegaName(illegaName);
	}

	// --> start
	private SensitiveWordUtils illegaWord = new SensitiveWordUtils();// 关键字过滤
	private SensitiveWordUtils illegaName = new SensitiveWordUtils();// 名字中不能包含的

	// <-- end

	public boolean isIllegaWord(String value) {
		SensitiveWordUtils filter = getIllegaWord();
		return filter.containsSensitiveWorld(value);
	}

	public boolean isIllegaName(String value) {
		SensitiveWordUtils filter = getIllegaName();
		return isIllegaWord(value) || filter.containsSensitiveWorld(value);
	}

	public String filterIllegaWord(String value) {
		SensitiveWordUtils filter = getIllegaWord();
		return filter.replaceSensitiveWorld(value, '*');
	}

	public SensitiveWordUtils getIllegaWord() {
		return illegaWord;
	}

	public void setIllegaWord(SensitiveWordUtils illegaWord) {
		this.illegaWord = illegaWord;
	}

	public SensitiveWordUtils getIllegaName() {
		return illegaName;
	}

	public void setIllegaName(SensitiveWordUtils illegaName) {
		this.illegaName = illegaName;
	}
}
